﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Delay;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using Microsoft.Phone.Shell;
using System.IO;
using Microsoft.Xna.Framework.Media;

namespace PhoneInfo.Page
{

    public partial class TypeData : PhoneApplicationPage
    {
        private string pid;
        private string ppname;
        private string picurl;
        private string ppid;
        private string phonename;
        private TypeParameter typeParameter = null;
        private TypePrice typePrice = null;
        private TypePicture typePicture = null;
        private TypeCommentView typeComment = null;
        private ScoreDetail scDetail = null;
        private static int lastPage = -1;
        private int imageIndex = -1;
        BibleView bibleViewMarket = null;
        BibleView bibleViewTest = null;
        BibleView bibleViewSkill = null;
        BibleView bibleViewBug = null;
        BibleView bibleViewBuyPhone = null;
        private int currentBible = -1;
        public TypeData()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //取参数，机型合并
            //在这里进行处理
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;
            if (parameters.ContainsKey("pid")) pid = parameters["pid"];
            if (parameters.ContainsKey("price"))  Price.Text = parameters["price"];
            if (parameters.ContainsKey("maxprice"))
            {
                string minprice = parameters["maxprice"];
                if (minprice == "0") minprice = "暂无";
                MinPrice.Text = minprice;
            }
            if (parameters.ContainsKey("minprice"))
            {
                string minprice = parameters["minprice"];
                if (minprice == "0")  minprice = "暂无";
                MaxPrice.Text = minprice;
            }
            if (parameters.ContainsKey("picurl"))
            {
                picurl = parameters["picurl"];
                defualtImage.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(parameters["picurl"]));
            }
            if (parameters.ContainsKey("phonename"))  phonename = parameters["phonename"];
            if (parameters.ContainsKey("ppname"))
            {
                ppname = parameters["ppname"];
                typePhoneName.Text = ppname ;
                if (ppname != null && ppname.Length > 0 && phonename != null && phonename.Length > 0)
                {
                    if (ppname.IndexOf(phonename) < 0)  typePhoneName.Text +=" "+ phonename;
                }
            }
            if (parameters.ContainsKey("ppid")) ppid = parameters["ppid"];
            if (typeParameter == null)
            {
                typeParameter = new TypeParameter();
                ProgBar.Visibility = Visibility.Visible;
                typeParameter.ImageVis = false;
                typeParameter.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                typeParameter.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typeParameter.GetPhoneInfoUrl(pid)));
                Paramater.DataContext = typeParameter;
            }
        }
        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender == null) return;
            lastPage = -1;
            Pivot pv = (Pivot)sender;
            switch (pv.SelectedIndex)
            {
                case 0:
                    //参数
                    ApplicationBar = null;
                    break;
                case 1:
                    ApplicationBar = null;
                    if (typePrice == null)
                    {
                        typePrice = new TypePrice();
                        ProgBar.Visibility = Visibility.Visible;
                        typePrice.Result = 0;
                        typePrice.CurrentPage = 1;
                        typePrice.AllowAdd = false;
                        typePrice.PageSize = 10;
                        typePrice.TotalPage = 0;
                        typePrice.Result = 0;
                        typePrice.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typePrice.GetPhoneInfoUrl(pid, typePrice.CurrentPage)));
                        typePrice.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        TypePrice.DataContext = typePrice;
                    }
                    //报价
                    break;
                case 2:
                    //点评
                    ApplicationBar = null;
                    if (typeComment == null)
                    {
                        typeComment = new TypeCommentView();
                        ProgBar.Visibility = Visibility.Visible;
                        typeComment.Result = 0;
                        typeComment.CurrentPage = 1;
                        typeComment.AllowAdd = false;
                        typeComment.PageSize = 10;
                        typeComment.TotalPage = 0;
                        typeComment.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typeComment.GetPhoneInfoUrl(pid, typeComment.CurrentPage)));
                        typeComment.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        Comment.DataContext = typeComment;
                    }
                    if (scDetail == null)
                    {
                        scDetail = new ScoreDetail();
                        scDetail.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        scDetail.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(scDetail.GetPhoneInfoUrl(pid)));
                    }
                    break;
                case 3:
                    if (typePicture == null)
                    {
                        typePicture = new TypePicture();
                        ProgBar.Visibility = Visibility.Visible;
                        typePicture.ImageVis = false;
                        typePicture.CurrentPage = 1;
                        typePicture.AllowAdd = false;
                        typePicture.PageSize = 20;
                        typePicture.TotalPage = 0;
                        typePicture.Result = 0;
                        typePicture.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typePicture.GetPhoneInfoUrl(pid, typePicture.CurrentPage)));
                        typePicture.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        Picture.DataContext = typePicture;
                    }
                    //图片
                    break;
                case 4:
                    //宝典
                    if (bibleViewMarket == null)
                    {
                        bibleViewMarket = new BibleView();
                        ProgBar.Visibility = Visibility.Visible;
                        bibleViewMarket.Result = 0;
                        bibleViewMarket.CurrentPage = 1;
                        bibleViewMarket.AllowAdd = false;
                        bibleViewMarket.PageSize = 20;
                        bibleViewMarket.TotalPage = 0;
                        bibleViewMarket.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(bibleViewMarket.GetPhoneInfoUrl(bibleViewMarket.CurrentPage, 0, pid)));
                        bibleViewMarket.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        BibleMarket.DataContext = bibleViewMarket;
                    }
                    ApplicationBar = null;
                    break;
            }
        }
        void type_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) return;
            if (sender is TypePrice)
            {
                TypePrice tp = sender as TypePrice;
                if (tp == null)
                    return;
                else if (e.PropertyName == "CurrentPage")
                    lastPage = tp.CurrentPage;
                else if (e.PropertyName == "Result")
                {
                    if (tp.Result==1)
                        priceResult.Visibility = Visibility.Visible;
                    ProgBar.Visibility = Visibility.Collapsed;
                }
            }
            else if (sender is TypeParameter)
            {
                TypeParameter tp = sender as TypeParameter;
                if (tp == null) return;
                if (e.PropertyName == "ImageVis")
                {
                    if (tp.ImageVis == true) ProgBar.Visibility = Visibility.Collapsed;//
                }
            }
            else if (sender is TypePicture)
            {
                TypePicture tp = sender as TypePicture;
                ProgBar.Visibility = Visibility.Collapsed;
                if (tp == null) return;
                if (e.PropertyName == "Result")
                {
                    //进行处理
                    ProgBar.Visibility = Visibility.Collapsed;//
                    switch (tp.Result)
                    {
                        case -1:
                            picResult.Text = "网络错误，请稍后重试！";
                            picResult.Visibility = Visibility.Visible;
                            break;
                        case 1:
                            picResult.Text = "没有您要查找的信息";
                            picResult.Visibility = Visibility.Visible;
                            break;
                        case 2:
                            picResult.Visibility = Visibility.Collapsed;
                            break;
                    }
                }
            }
            else if (sender is TypeCommentView)
            {
                TypeCommentView tp = sender as TypeCommentView;
                if (tp == null) return;
                if (e.PropertyName == "Result")
                {
                    switch (tp.Result)
                    {
                        case -1:
                        case 1:
                            ProgBar.Visibility = Visibility.Collapsed;
                            CommetResult.Visibility = Visibility.Visible;
                            break;
                        case 2:
                            ProgBar.Visibility = Visibility.Collapsed;
                            break;
                    }
                }
                if (e.PropertyName == "Avgscore")
                {
                    float tmp = float.Parse(tp.Avgscore);
                    ScoreStart.Marked = (int)tmp;
                    scoreText.Text = tmp.ToString("#.#");
                }
            }
            else if (sender is ScoreDetail)
            {
                ScoreDetail tp = sender as ScoreDetail;
                if (tp == null) return;
                if (e.PropertyName == "TotalScore")
                {
                    if (tp.TotalScore != 0)
                    {
                        sc1.Value = (float)tp.Sc1 * 100 / tp.TotalScore;                        //
                        sc2.Value = (float)tp.Sc2 * 100 / tp.TotalScore;                        //
                        sc3.Value = (float)tp.Sc3 * 100 / tp.TotalScore;                        //
                        sc4.Value = (float)tp.Sc4 * 100 / tp.TotalScore;                        //
                        sc5.Value = (float)tp.Sc5 * 100 / tp.TotalScore;
                        Textsc1.Text = tp.Sc1.ToString() + "人";
                        Textsc2.Text = tp.Sc2.ToString() + "人";
                        Textsc3.Text = tp.Sc3.ToString() + "人";
                        Textsc4.Text = tp.Sc4.ToString() + "人";
                        Textsc5.Text = tp.Sc5.ToString() + "人";
                        float avg = (float)(tp.Sc1 * 1.0 + tp.Sc2 * 2.0 + tp.Sc3 * 3.0 + tp.Sc4 * 4.0 + tp.Sc5 * 5.0) / tp.TotalScore;
                        scoreText.Text = avg.ToString("#.#");
                        ScoreStart.Marked = (int)avg;
                    }
                    else
                    {
                        scoreText.Text = "0星";
                        ScoreStart.Marked = 0;
                    }
                }
            }
            else if (sender is BibleView)
            {
                BibleView tp = sender as BibleView;
                if (tp == null) return;
                if (e.PropertyName == "Result")
                {
                    ProgBar.Visibility = Visibility.Collapsed;
                    if (tp.Result == 1 || tp.Result == -1)
                    {
                        switch (BibleTab.SelectedIndex)
                        {
                            case 0:
                                //行情
                                TextBibleMarket.Visibility = Visibility.Visible;
                                break;
                            case 1:
                                //测评
                                TextBibleTest.Visibility = Visibility.Visible;
                                break;
                            case 2:
                                //技能
                                TextBibleSkill.Visibility = Visibility.Visible;
                                break;
                            case 3:
                                //bug
                                TextBibleBug.Visibility = Visibility.Visible;
                                break;
                            case 4:
                                TextBibleBuy.Visibility = Visibility.Visible;
                                break;
                        }
                    }
                    else
                    {
                        switch (BibleTab.SelectedIndex)
                        {
                            case 0:
                                //行情
                                TextBibleMarket.Visibility = Visibility.Collapsed;
                                break;
                            case 1:
                                //测评
                                TextBibleTest.Visibility = Visibility.Collapsed;
                                break;
                            case 2:
                                //技能
                                TextBibleSkill.Visibility = Visibility.Collapsed;
                                break;
                            case 3:
                                //bug
                                TextBibleBug.Visibility = Visibility.Collapsed;
                                break;
                            case 4:
                                TextBibleBuy.Visibility = Visibility.Collapsed;
                                break;
                        }
                    }
                }
            }
        }
        private void verticalScrollBar_ValueChanged(object sender, RoutedEventArgs e)
        {
            ScrollBar scrollBar = (ScrollBar)sender;
            object valueObj = scrollBar.GetValue(ScrollBar.ValueProperty);
            object maxObj = scrollBar.GetValue(ScrollBar.MaximumProperty);
            Debug.WriteLine("ValueProperty si {0}", valueObj);
            Debug.WriteLine("MaximumProperty si {0}", maxObj);
            if (valueObj != null && maxObj != null)
            {
                double value = (double)valueObj;
                double max = (double)maxObj - 1.5;
                if (value >= max)
                {
                    PivotItem currentPivot = mainPivot.SelectedItem as PivotItem;
                    if (currentPivot == null) return;
                    if (currentPivot.DataContext is TypePrice)
                    {
                        TypePrice currentItem = currentPivot.DataContext as TypePrice;
                        if (lastPage == currentItem.CurrentPage) return;
                        //已经修改模型事件的回调函数中更改防止 同一页面加载2次
                        if (currentItem.CurrentPage * currentItem.PageSize <= currentItem.TotalPage)
                        {
                            ProgBar.Visibility = Visibility.Visible;
                            currentItem.Result = 0;
                            currentItem.AllowAdd = true;
                            currentItem.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typePrice.GetPhoneInfoUrl(pid, currentItem.CurrentPage + 1)));
                        }
                    }
                    else if (currentPivot.DataContext is TypeCommentView)
                    {
                        TypeCommentView currentItem = currentPivot.DataContext as TypeCommentView;
                        Debug.WriteLine("Load  TypeCommentView last page is {0} current page is{1}", lastPage, currentItem.CurrentPage);
                        if (lastPage == currentItem.CurrentPage) return;
                        Debug.WriteLine("Loadding  TypeCommentView  current page is{0}", currentItem.CurrentPage);
                        lastPage = currentItem.CurrentPage;
                        //已经修改模型事件的回调函数中更改防止 同一页面加载2次
                        if (currentItem.CurrentPage * currentItem.PageSize <= currentItem.TotalPage)
                        {
                            ProgBar.Visibility = Visibility.Visible;
                            currentItem.Result = 0;
                            currentItem.AllowAdd = true;
                            currentItem.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(currentItem.GetPhoneInfoUrl(pid, currentItem.CurrentPage + 1)));
                        }

                    }
                    if (mainPivot.SelectedIndex == 4)
                    {
                        switch (BibleTab.SelectedIndex)
                        {
                            case 0:
                                if (bibleViewMarket != null)
                                {
                                    if (bibleViewMarket.CurrentPage * bibleViewMarket.PageSize <= bibleViewMarket.TotalPage)
                                    {
                                        if (currentBible == bibleViewMarket.CurrentPage) return;
                                        currentBible = bibleViewMarket.CurrentPage;
                                        ProgBar.Visibility = Visibility.Visible;
                                        bibleViewMarket.AllowAdd = true;
                                        bibleViewMarket.SetValue(LowProfileXmlLoader.UriSourceProperty,
                                                               new Uri(bibleViewMarket.GetPhoneInfoUrl(
                                                                   bibleViewMarket.CurrentPage + 1, 5, pid)));
                                    }
                                }
                                break;
                            case 1:
                                //bibleViewTest
                                if (bibleViewTest != null)
                                {
                                    if (bibleViewTest.CurrentPage * bibleViewTest.PageSize <= bibleViewTest.TotalPage)
                                    {
                                        if (currentBible == bibleViewTest.CurrentPage) return;
                                        currentBible = bibleViewTest.CurrentPage;
                                        ProgBar.Visibility = Visibility.Visible;
                                        bibleViewTest.AllowAdd = true;
                                        bibleViewTest.SetValue(LowProfileXmlLoader.UriSourceProperty,
                                                               new Uri(bibleViewTest.GetPhoneInfoUrl(bibleViewTest.CurrentPage + 1, 5, pid)));
                                    }
                                }
                                break;
                            case 2:
                                //bibleViewSkill
                                if (bibleViewSkill != null)
                                {
                                    if (bibleViewSkill.CurrentPage * bibleViewSkill.PageSize <= bibleViewSkill.TotalPage)
                                    {
                                        if (currentBible == bibleViewSkill.CurrentPage) return;
                                        currentBible = bibleViewSkill.CurrentPage;
                                        ProgBar.Visibility = Visibility.Visible;
                                        bibleViewSkill.AllowAdd = true;
                                        bibleViewSkill.SetValue(LowProfileXmlLoader.UriSourceProperty,
                                                               new Uri(bibleViewSkill.GetPhoneInfoUrl(bibleViewSkill.CurrentPage + 1, 5, pid)));
                                    }
                                }
                                break;
                            case 3:
                                //bibleViewBug
                                if (bibleViewBug != null)
                                {
                                    if (currentBible == bibleViewBug.CurrentPage) return;
                                    currentBible = bibleViewBug.CurrentPage;
                                    if (bibleViewBug.CurrentPage * bibleViewBug.PageSize <= bibleViewBug.TotalPage)
                                    {
                                        ProgBar.Visibility = Visibility.Visible;
                                        bibleViewBug.AllowAdd = true;
                                        bibleViewBug.SetValue(LowProfileXmlLoader.UriSourceProperty,
                                                               new Uri(bibleViewBug.GetPhoneInfoUrl(
                                                                   bibleViewBug.CurrentPage + 1, 5, pid)));
                                    }
                                }
                                break;
                            case 4:
                                if (bibleViewBuyPhone != null)
                                {
                                    if (bibleViewBuyPhone.CurrentPage * bibleViewBuyPhone.PageSize <= bibleViewBuyPhone.TotalPage)
                                    {
                                        if (currentBible == bibleViewBuyPhone.CurrentPage) return;
                                        currentBible = bibleViewBuyPhone.CurrentPage;
                                        ProgBar.Visibility = Visibility.Visible;
                                        bibleViewBuyPhone.AllowAdd = true;
                                        bibleViewBuyPhone.SetValue(LowProfileXmlLoader.UriSourceProperty,
                                                               new Uri(bibleViewBuyPhone.GetPhoneInfoUrl(
                                                                   bibleViewBuyPhone.CurrentPage + 1, 5, pid)));
                                    }

                                }
                                break;
                        }
                    }
                }
            }
        }
        public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement
        {
            List<T> visualCollection = new List<T>();
            GetVisualChildCollection(parent as DependencyObject, visualCollection);
            return visualCollection;
        }
        private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : UIElement
        {
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                    visualCollection.Add(child as T);
                else if (child != null)
                    GetVisualChildCollection(child, visualCollection);
            }
        }
        private void FirstListBox_Loaded(object sender, RoutedEventArgs e)
        {
            List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(sender);
            foreach (ScrollBar scrollBar in scrollBarList)
            {
                if (scrollBar.Orientation != System.Windows.Controls.Orientation.Horizontal)
                    scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar_ValueChanged);
            }
        }
        private void Conmit_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null) return;
            string defect = Advant.Text;
            string advant = Defect.Text;
            if (defect.Length < 6 || defect.Length>120 )
            {
                MessageBox.Show("缺点必须为6至120个字！");
                return;
            }
            if (advant.Length < 6 || advant.Length > 120)
            {
                MessageBox.Show("优点必须为6至120个字！");
                return;
            }
            //等事件完成了再唤出工具条
            ProgBarPop.Visibility = Visibility.Visible;
            Advant.IsEnabled = false;
            Defect.IsEnabled = false;
            ((Button)sender).IsEnabled = false;
            int startNumber = StartControl.Marked;
            ReplyResult rpResut = new ReplyResult();
            rpResut.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(rpResut.GetPhoneInfoUrl(pid, startNumber, advant, defect)));
            rpResut.PropertyChanged += new PropertyChangedEventHandler(rpResut_PropertyChanged);
        }
        void rpResut_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) return;
            ReplyResult se = (ReplyResult)sender;
            if (e.PropertyName == "Result")
            {
                if (se.Result == 1 || se.Result == 2)
                {//成功
                    ProgBarPop.Visibility = Visibility.Collapsed;
                    Advant.IsEnabled = true;
                    Defect.IsEnabled = true;
                    PopFull.IsOpen = false;
                    Conmmit.IsEnabled = true;

                    typeComment = new TypeCommentView();
                    ProgBar.Visibility = Visibility.Visible;
                    typeComment.Result = 0;
                    typeComment.CurrentPage = 1;
                    typeComment.AllowAdd = false;
                    typeComment.PageSize = 10;
                    typeComment.TotalPage = 0;
                    typeComment.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typeComment.GetPhoneInfoUrl(pid, typeComment.CurrentPage)));
                    typeComment.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                    Comment.DataContext = typeComment;

                    new ToastPrompt() { Message = "点评成功！" }.Show();
                }
                else
                {//失败
                    ProgBarPop.Visibility = Visibility.Collapsed;
                    Advant.IsEnabled = true;
                    Defect.IsEnabled = true;
                    Conmmit.IsEnabled = true;
                    MessageBox.Show("提交点评失败，点确定退出，点取消继续重试", "提示", MessageBoxButton.OK | MessageBoxButton.OKCancel);
                }
            }
        }
        private void Into_Full_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null) return;
            PopFull.IsOpen = true;
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //这里可以执行弹出对话框操作
            if (PopFull.IsOpen == true)
            {
                ProgBarPop.Visibility = Visibility.Collapsed;
                Advant.IsEnabled = true;
                Defect.IsEnabled = true;
                Conmmit.IsEnabled = true;
                PopFull.IsOpen = false;
                e.Cancel = true;
            }
            if (ImagePoP.IsOpen == true)
            {
                ImagePoP.IsOpen = false;
                image.Source = null;
                imageIndex = -1;
                e.Cancel = true;
                ApplicationBar = null;
            }
            if (PopShard.IsOpen == true)
            {
                PopShard.IsOpen = false;
                e.Cancel = true;
            }
        }
        private void save(object sender, EventArgs e)
        {
            using (Stream stream = LowProfileXmlLoader.LoadCachedImage(new Uri(typePicture.TypePictureItems[imageIndex].Acturl)))
            {
                if (stream == null)
                {
                    ToastPrompt toast = new ToastPrompt() { Message = "读取图片失败！" };
                    return;
                }
                else
                {
                    MediaLibrary mediaLibrary = new MediaLibrary();
                    int end = typePicture.TypePictureItems[imageIndex].Acturl.LastIndexOf(".");
                    if (end < 0)
                    {
                        ToastPrompt toast = new ToastPrompt() { Message = "图片格式有错误！" };
                        return;
                    }
                    string endfilename = typePicture.TypePictureItems[imageIndex].Acturl.Substring(end);
                    string tempUmd = MD5.GetMd5String(LowProfileXmlLoader.GetParsePath(typePicture.TypePictureItems[imageIndex].Acturl));
                    string fileName = tempUmd + endfilename;
                    Picture pic = mediaLibrary.SavePicture(fileName, stream);
                    MessageBox.Show("图片保存成功");
                }
            }
        }
        private void PreviousPage(object sender, EventArgs e) { }
        private void NextPage(object sender, EventArgs e) { }
        private void ListBoxImage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox list = sender as ListBox;
            if (list == null) return;
            TypePictureItem item = list.SelectedItem as TypePictureItem;
            imageIndex = list.SelectedIndex;
            if (item == null) return;
            ApplicationBar = null;
            ImagePoP.IsOpen = true;
            if (App.appSitting.Theme == 1)
                ApplicationBar = (ApplicationBar)this.Resources["AppBarSavePic"];
            else
                ApplicationBar = (ApplicationBar)this.Resources["AppBarSavePic1"];
            image.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(item.Acturl));
        }
        private void Popup_Flick(object sender, FlickGestureEventArgs e)
        {
            if (sender == null || typePicture == null) return;
            if (e.HorizontalVelocity < 0)
            {
                if (imageIndex < typePicture.TypePictureItems.Count - 1)
                {
                    imageIndex += 1;
                    image.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typePicture.TypePictureItems[imageIndex].Acturl));
                }
            }
            else if (e.HorizontalVelocity > 0)
            {//向右滑动
                if (imageIndex > 0)
                {//向左滑动
                    imageIndex -= 1;
                    image.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(typePicture.TypePictureItems[imageIndex].Acturl));
                }
            }

        }
        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender == null) return;
            TabControl tabControl = sender as TabControl;
            if (tabControl == null) return;
            switch (tabControl.SelectedIndex)
            {
                case 0:
                    break;
                case 1:
                    //5 评测
                    if (bibleViewTest == null)
                    {
                        bibleViewTest = new BibleView();
                        ProgBar.Visibility = Visibility.Visible;
                        bibleViewTest.Result = 0;
                        bibleViewTest.CurrentPage = 1;
                        bibleViewTest.AllowAdd = false;
                        bibleViewTest.PageSize = 20;
                        bibleViewTest.TotalPage = 0;
                        bibleViewTest.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(bibleViewTest.GetPhoneInfoUrl(bibleViewTest.CurrentPage, 5, pid)));
                        bibleViewTest.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        BibleTest.DataContext = bibleViewTest;
                    }
                    break;
                case 2:
                    //1 技巧
                    if (bibleViewSkill == null)
                    {
                        bibleViewSkill = new BibleView();
                        ProgBar.Visibility = Visibility.Visible;
                        bibleViewSkill.Result = 0;
                        bibleViewSkill.CurrentPage = 1;
                        bibleViewSkill.AllowAdd = false;
                        bibleViewSkill.PageSize = 20;
                        bibleViewSkill.TotalPage = 0;
                        bibleViewSkill.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(bibleViewSkill.GetPhoneInfoUrl(bibleViewSkill.CurrentPage, 1, pid)));
                        bibleViewSkill.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        BibleSkill.DataContext = bibleViewSkill;
                    }
                    break;
                case 3:
                    //3 BUG
                    if (bibleViewBug == null)
                    {
                        bibleViewBug = new BibleView();
                        ProgBar.Visibility = Visibility.Visible;
                        bibleViewBug.Result = 0;
                        bibleViewBug.CurrentPage = 1;
                        bibleViewBug.AllowAdd = false;
                        bibleViewBug.PageSize = 20;
                        bibleViewBug.TotalPage = 0;
                        bibleViewBug.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(bibleViewBug.GetPhoneInfoUrl(bibleViewBug.CurrentPage, 3, pid)));
                        bibleViewBug.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        BibleBug.DataContext = bibleViewBug;
                    }
                    break;
                case 4:
                    if (bibleViewBuyPhone == null)
                    {
                        bibleViewBuyPhone = new BibleView();
                        ProgBar.Visibility = Visibility.Visible;
                        bibleViewBuyPhone.Result = 0;
                        bibleViewBuyPhone.CurrentPage = 1;
                        bibleViewBuyPhone.AllowAdd = false;
                        bibleViewBuyPhone.PageSize = 20;
                        bibleViewBuyPhone.TotalPage = 0;
                        bibleViewBuyPhone.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(bibleViewBuyPhone.GetPhoneInfoUrl(bibleViewBuyPhone.CurrentPage, 4, pid)));
                        bibleViewBuyPhone.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        BibleBuyphone.DataContext = bibleViewBuyPhone;
                    }
                    //4 购机
                    break;
            }
        }
        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {//点击进入文章列表
            if (sender == null) return;
            ListBox lb = sender as ListBox;
            if (lb == null) return;
            PhoneInfoCenterItem itemInList = lb.SelectedItem as PhoneInfoCenterItem;
            if (itemInList == null) return;
            string objective = "/Page/ContentAndReply.xaml";
            if (itemInList.Topicid == null)
            {
                MessageBox.Show("该帖子ID有错误，请刷新后重试！");
                return;
            }
            objective += "?topicid=" + itemInList.Topicid;
            objective += "&readcount=" + itemInList.Readcount;
            objective += "&replycount=" + itemInList.Replycount;
            string IsBible = "0";
            if (BibleTab.SelectedIndex != 0) { IsBible = "1"; }
            objective += "&IsBible=" + IsBible;
            objective += "&title=" + itemInList.Title;
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (PopShard.IsOpen == true)
                PopShard.IsOpen = false;
        }
        private void BtnPK_Jia(object sender, RoutedEventArgs e)
        {
            string page = "/Page/SearchResult.xaml";
            page += "?ppname_1=" + ppname;
            page += "&pid_1=" + pid;
            page += "&ppid_1=" + ppid;
            page += "&price_1=" + Price.Text;
            page += "&picurl_1=" + picurl;
            page += "&phonename_1=" + phonename;
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void BtnPK_Pin(object sender, RoutedEventArgs e)
        {
            string page = "/Page/SearchResult.xaml";
            page += "?ppname_1=" + ppname;
            page += "&pid_1=" + pid;
            page += "&price_1=" + Price.Text;
            page += "&picurl_1=" + picurl;
            page += "&phonename_1=" + phonename;
            string xs = "";
            foreach (PhoneInfoCenterItem i in typeParameter.TypeParameterItems)
            {
                if (i.Title == "主屏分辨率")
                    xs = i.Content.Replace("像素", "").Replace("X", "*").Replace("x", "*");
            }
            int tag = 0;
            if (xs == "240*320" || xs == "320*240") tag = 1;
            else if (xs == "320*480" || xs == "480*320") tag = 2;
            else if (xs == "360*640" || xs == "640*360") tag = 3;
            else if (xs == "480*800" || xs == "800*480") tag = 4;
            else if (xs == "480*854" || xs == "854*480") tag = 5;
            else if (xs == "540*960" || xs == "960*540") tag = 6;
            else if (xs == "640*960" || xs == "960*640") tag = 7;
            else if (xs == "800*1280" || xs == "1280*800") tag = 8;
            page += "&distingwish=" + (tag == 0 ? "" : tag.ToString());
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void BtnPK_Fen(object sender, RoutedEventArgs e)
        {
            string page = "/Page/SearchResult.xaml";
            page += "?ppname_1=" + ppname;
            page += "&pid_1=" + pid;
            page += "&price_1=" + Price.Text;
            page += "&picurl_1=" + picurl;
            page += "&phonename_1=" + phonename;
            string xs = "";
            foreach (PhoneInfoCenterItem i in typeParameter.TypeParameterItems)
            {
                if (i.Title == "主屏分辨率")
                    xs = i.Content.Replace("像素", "").Replace("X", "*").Replace("x", "*");
            }
            int tag = 0;
            if (xs == "240*320" || xs == "320*240") tag = 1;
            else if (xs == "320*480" || xs == "480*320") tag = 2;
            else if (xs == "360*640" || xs == "640*360") tag = 3;
            else if (xs == "480*800" || xs == "800*480") tag = 4;
            else if (xs == "480*854" || xs == "854*480") tag = 5;
            else if (xs == "540*960" || xs == "960*540") tag = 6;
            else if (xs == "640*960" || xs == "960*640") tag = 7;
            else if (xs == "800*1280" || xs == "1280*800") tag = 8;
            page += "&distingwish=" + (tag == 0 ? "" : tag.ToString());
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void BtnPK_Xi(object sender, RoutedEventArgs e)
        {
            string page = "/Page/SearchResult.xaml";
            page += "?ppname_1=" + ppname;
            page += "&pid_1=" + pid;
            page += "&price_1=" + Price.Text;
            page += "&picurl_1=" + picurl;
            page += "&phonename_1=" + phonename;
            string xs2 = "";
            foreach (PhoneInfoCenterItem i in typeParameter.TypeParameterItems)
            {
                if (i.Title == "操作系统")
                    xs2 = i.Content.ToLower();
            }
            int tag2 = 0;
            if (xs2.IndexOf("symbian") > -1) tag2 = 1;
            else if (xs2.IndexOf("android") > -1) tag2 = 2;
            else if (xs2.IndexOf("windows phone") > -1) tag2 = 8;
            else if (xs2.IndexOf("Linux") > -1) tag2 = 4;
            else if (xs2.IndexOf("MTK") > -1) tag2 = 5;
            else if (xs2.IndexOf("其它") > -1) tag2 = 6;
            else if (xs2.IndexOf("Apple") > -1) tag2 = 7;
            else if (xs2.IndexOf("windows") > -1) tag2 = 3;
            page += "&system=" + (tag2 == 0 ? "" : tag2.ToString());
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void BtnPK_Qita(object sender, RoutedEventArgs e)
        {
            string page = "/Page/MobileProduct.xaml";
            page += "?ispk=1&ppname_1=" + ppname;
            page += "&pid_1=" + pid;
            page += "&ppid_1=" + ppid;
            page += "&price_1=" + Price.Text;
            page += "&picurl_1=" + picurl;
            page += "&phonename_1=" + phonename;
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void PK(object sender, RoutedEventArgs e)
        {
            ApplicationBar = null;
            PopShard.IsOpen = true;
        }
        private void lexun_bbs(object sender, RoutedEventArgs e)
        {
            string url = "http://sjgs.lexun.com/bbs_listbymodel.aspx?pid=" + pid;
            string pageName = "/Page/LexunMap.xaml";
            pageName += "?url=" + url;
            NavigationService.Navigate(new Uri(pageName, UriKind.Relative));
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            ImagePoP.IsOpen = false;
            image.Source = null;
            imageIndex = -1;
            ApplicationBar = null;
        }
    }
}